package com.example.demo;

import cn.hutool.core.util.StrUtil;

import java.io.ByteArrayOutputStream;

public class Test {
    private static String hexString = "0123456789ABCDEFabcdef";
    public static void main(String[] args) {
        String msg = "{\n" +
                "  \"content\": {\n" +
                "      \"sending-rate\":\"4657\",\n" +
                "      \"receiving-rate\":\"18295\",\n" +
                "      \"signal\":\"18.2\"\n" +
                "  },\n" +
                "  \"time\": 1618366049124\n" +
                "}";
        byte[] bytes = msg.getBytes();
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        //转换hex编码
        for (byte b : bytes) {
            sb.append(Integer.toHexString(b + 0x800).substring(1));

        }

//转换后的代码为c7d7a3acc4e3bac3
        msg = sb.toString();
        System.out.println(msg);

        //再次转换为string
        System.out.println(decode(msg));
        //System.out.println(msg);
    }



//把hex编码转换为string

    public static String decode(String bytes) {
        bytes = bytes.toUpperCase();
        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
        // 将每2位16进制整数组装成一个字节
        for (int i = 0; i < bytes.length(); i += 2) {
            baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
        }
        return new String(baos.toByteArray());
    }
}
